layui.config({
	base : "js/"
}).use(['form','layer','jquery','layedit','laydate'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		layedit = layui.layedit,
		laydate = layui.laydate,
		$ = layui.jquery;

	//创建一个编辑器
	
	
 	var orgId = window.sessionStorage.getItem("organizeId");
 	var tmsgId = window.sessionStorage.getItem("tempMsgEditId");
 	
	
 	$.ajax({
		url: $.httpRootPath() + "/api/web/tempMsg/i/" + orgId + "/" + tmsgId,
		type: "get",
		dataType: "json",
		success: function(data) {
			if(data.code == "00") {
				fillData(data.data);
//				if(data.data.state == '1'){
//					layer.msg("该信息已推送，不允许修改！");
//					$("#submitBtn").attr("style","display: none;");
//				}
			} else {
				layer.alert('加载失败', {
					icon: 6,
					title: '友情提示'
				});
			}

		}
	})
 	
 	function fillData(data){
 		$(".title").val(data.title),
       	$(".content").val(data.content),
       	$(".remark").val(data.remark),
       	$(".url").val(data.url)
 	}
 	
 	form.on("submit(subInfo)",function(data){
 		//弹出loading
 		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
   		var data = {
        	title: $(".title").val(),
       		content:$(".content").val(),
       		remark:$(".remark").val(),
       		url:$(".url").val(),
       		orgId:orgId,
       		tmsgId:tmsgId,
       		msgType:"测试"
   		 };
 		updateInfo(data,function(){
 			top.layer.close(index);
 			layer.closeAll("iframe");
	 		//刷新父页面
	 		parent.location.reload();
 		})
 		return false;
 	})
})

function updateInfo(data,callback) {
    $.ajax({
        url: $.httpRootPath()+'/api/web/tempMsg/i/'+data.orgId+"/"+data.tmsgId,
        type: 'PUT',
        dataType: 'json',
        data: data,
        timeout: 15000,
        cache: false,
        success: function (r) {
            if (r.code == "00") {
              	layer.msg("更新成功！");
            } else {
               layer.msg("更新失败！");
            }
        },
        error: function (e) {
          layer.msg("系统错误！");
        },
        complete:function(){
        	callback();	
        }
    });

}
